package com.ht.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.data.Result;
import com.ht.domain.bo.DoctorBO;
import com.ht.domain.po.TbDoctor;
import com.ht.dto.TbDoctorDTO;
import com.ht.em.ResultEnum;
import com.ht.mapper.DoctorMapper;
import com.ht.service.DoctorService;
import com.ht.util.ShiroUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 医生表service
 */
@Service
public class DoctorServiceImpl implements DoctorService {

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 医生注册
     * @param doctorBO
     * @param verify
     * @return
     */
    @Override
    public Result doctorRegister(DoctorBO doctorBO, String verify) {
        String verifyStr = (String) redisTemplate.boundValueOps("phone_" + doctorBO.getDoctorPhone()).get();
        if (StrUtil.isBlank(verifyStr) || !verify.equals(verifyStr)){
            return Result.response(ResultEnum.NO_VERIFY);
        }
        TbDoctor tbDoctor = new TbDoctor();
        BeanUtils.copyProperties(doctorBO,tbDoctor);
        tbDoctor.setAuditStatus(0);
        //获取盐
        String salt = ShiroUtils.generateSalt(5);
        //获取密码
        String password = ShiroUtils.encryptPassword("MD5", tbDoctor.getPassword(), salt, 3);
        tbDoctor.setSalt(salt);
        tbDoctor.setPassword(password);
        int i = doctorMapper.insert(tbDoctor);
        if (i <= 0){
            return Result.ERROR();
        }
        return Result.OK();
    }
}
